﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace geetol.sms.core
{
    public class weimei_sms_api
    {

        api_info apiinfo;

        public weimei_sms_api()
        {
            apiinfo = config.LoadApi_Info();
        }

        public res_SendSMS SendSMS(string mobile,string msg)
        {
            Logs log = new Logs("sendsms");
            log.Write("send -> " + mobile + ":" + msg);
            res_SendSMS r = new res_SendSMS()
            {
                sendtime = DateTime.Now.ToString(),
                status = "0",
                returnmsg = "测试保存日记没发的",
                taskid = DateTime.Now.ToString("yyyyMMddHHmmssffff")
            };
            return r;
            //if (apiinfo == null)
            //{
            //    res_SendSMS r = new res_SendSMS();
            //    r.status = "-99";
            //    r.returnmsg = "没有配置短信API信息";
            //    return r;
            //}
            //string url = apiinfo.url;
            //url += string.Format("SendSms.aspx?account={0}&pswd={1}&mobiles={2}&msg={3}",apiinfo.user,apiinfo.pwd, mobile,msg);
            //var s = toSend(url);
            //return Serialize.FromJson<res_SendSMS>(s);
        }

        public res_balance GetBalance()
        {
            if (apiinfo == null)
            {
                res_balance r = new res_balance();
                r.status = "-99";
                r.returnmsg = "没有配置短信API信息";
                return r;
            }
            string url = apiinfo.url;
            url += string.Format("GetBalance.aspx?account={0}&pswd={1}", apiinfo.user, apiinfo.pwd);
            var s= toSend(url);
            return Serialize.FromJson<res_balance>(s);

        }

        public string toSend(string url)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.AllowAutoRedirect = false;

                response = (HttpWebResponse)request.GetResponse();

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                    string html = reader.ReadToEnd();
                    return html;
                }
                else
                {
                    return "";
                }
            }
            catch (SystemException)
            {
                return "";
            }
        }

        public class res
        {
            public string status;
            public string returnmsg;
            
        }

        public class res_SendSMS : res
        {
            public string taskid;
            public string expenses;
            public string remaining;
            public string sendtime;
        }

        public class res_balance : res
        {
            public string balance;
        }

        public class api_info
        {
            public string url;
            public string user;
            public string pwd;
        }
    }
}
